package com.hxxy.wyp.petsLover.common.rest.base.backend;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.hxxy.wyp.petsLover.RestBase;
import com.hxxy.wyp.petsLover.common.entities.User;
import com.hxxy.wyp.petsLover.common.entities.vo.req.AdminAddReq;
import com.hxxy.wyp.petsLover.common.entities.vo.req.LoginInfo;
import com.hxxy.wyp.petsLover.common.entities.vo.resp.AuthInfo;
import com.hxxy.wyp.petsLover.common.services.base.backend.AdminService;
import com.hxxy.wyp.petsLover.common.services.base.fronted.UserService;
import com.hxxy.wyp.petsLover.security.SecurityContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @ClassName UserRestBase
 * @Description 管理员基类
 * @Author wyp
 * @Date 2022/12/13 17:19
 * @Version 1.0
 */
@RestController
@ResponseBody
@RequestMapping("/admin")
public class UserRestBase extends RestBase {
    @Autowired
    private SecurityContext securityContext;

    @Resource
    private AdminService adminService;

    @Resource
    private UserService userService;


    @RequestMapping("/user/login")
    public Object login(@Validated @RequestBody LoginInfo loginInfo) {
        return ajaxSuccess(adminService.login(loginInfo), "200", "成功");
    }

    /**
     * 获取当前用户信息
     */
    @RequestMapping(value = "/user/check", method = RequestMethod.GET)
    @ResponseBody
    @SaCheckLogin
    @SaCheckPermission(value = {"super_admin"}, mode = cn.dev33.satoken.annotation.SaMode.OR)
    public Object check(@RequestParam("token") String token) {
        if (securityContext.isLogin() && securityContext.getUserInfo() != null) {
            AuthInfo result = new AuthInfo();
            result.setAdmin(securityContext.getUserInfo());
            result.setPermissions(adminService.getAdminPermissions(securityContext.getLoginUserId()));
            result.setToken(StpUtil.getTokenValue());
            return ajaxSuccess(result);
        }
        return ajaxSuccess();
    }


    /**
     *
     */
    @RequestMapping("/user/all")
    @SaCheckLogin
    @SaCheckRole({"super_admin"})
    @SaCheckPermission("all")
    public Object selectAll() {
        return ajaxSuccess(adminService.selectAll(),"200","查询成功!");
    }

    @RequestMapping("/user/logout")
    public Object logout() {
        StpUtil.logout();
        return ajaxSuccess("200", "已登出!");
    }


    @RequestMapping("/user/kickout")
    @SaCheckLogin
    @SaCheckRole("super_admin")
    @SaCheckPermission("all")
    public Object kickout(@RequestParam("id") String id) {
        StpUtil.logout(id);
        User user = userService.getById(id);
        user.setIsOnline("0");
        userService.updateById(user);
        return ajaxSuccess("200", "该用户已经被踢下线!");

    }

    @RequestMapping("/user/add")
    @SaCheckLogin
    @SaCheckRole("super_admin")
    @SaCheckPermission("all")
    public Object addAdmin(@Validated @RequestBody AdminAddReq adminAddReq){
        Integer i = adminService.addAdmin(adminAddReq);
        if (i>0){
            return ajaxSuccess("200","新增成功!");
        }
        return ajaxError("500","新增失败");
    }

    @RequestMapping("/user/update")
    @SaCheckLogin
    @SaCheckRole("super_admin")
    @SaCheckPermission("all")
    public Object updateAdmin( @RequestBody AdminAddReq adminAddReq){
        Integer i = adminService.updateAdmin(adminAddReq);
        if (i>0){
            return ajaxSuccess("200","更新成功!");
        }
        return ajaxError("500","更新失败");
    }

    @RequestMapping("/user/delete")
    @SaCheckLogin
    @SaCheckRole("super_admin")
    @SaCheckPermission("all")
    public Object deleteAdmin(@RequestParam("id") String id){
        Integer i = adminService.deleteAdmin(id);
        if (i>0){
            return ajaxSuccess("200","更新成功!");
        }
        return ajaxError("500","更新失败");
    }


}
